<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

use Common\Model\Category;
/**
 * 首页控制器
 * 田昭鹏
 */

class IndexController extends Controller{
	private $data;
	//构造函数
	public function __init(){
		$this->data=new Category;
	}
	
    //动作
    public function index(){
        // p($_SESSION['cart']);
        // 导航区
    	// 获取导航条   内容
    	$menu=$this->data->where('is_hot',1)->limit(7)->get();
        foreach ($menu as $k => $v) {
            $menu[$k]['goods']=Db::table('goods')->where('category_cid',$v['cid'])->limit(2)->get();
        }
        // p($menu);
    	View::with('menu',$menu);

        // 获取数据  男装  女装
        $cateData=$this->data->where('pid',0)->limit(2)->get();
        // 重组数组
        foreach ($cateData as $k => $v) {
            $cateData[$k]['two']=$this->data->where('pid',$v['cid'])->get();
        }
        foreach ($cateData as $k => $v) {
            foreach ($v['two'] as $kk => $vv) {
                $cateData[$k]['two'][$kk]['three']=$this->data->where('pid',$vv['cid'])->get();
            }
        }
        // p($cateData);
        View::with('cateData',$cateData);

        // 获取数据 鞋袜
        $shoe=$this->data->where('cid',3)->get();
        // 声明空数组
        $shoeData=array();
        foreach ($shoe as $k => $v) {
            $shoeData=$this->data->where('pid',$v['cid'])->get();
        }
        View::with('shoeData',$shoeData);




        // 商品区
        // 当季推荐  商品
        $dangji=array();
            // 一排
        $dangji=Db::table('goods')->whereIn('category_cid',array(4,9,10,11,12))->limit(5)->get();
            // 二排
        $dangji=array_merge($dangji,Db::table('goods')->whereIn('category_cid',array(26,28,29,30))->limit(5)->get());
            // 三排
        $dangji=array_merge($dangji,Db::table('goods')->whereIn('category_cid',array(19,20,21,22))->limit(5)->get());
        // p($dangji);
        View::with('dangji',$dangji);
    	
        // 上装
        $shang=Db::table('goods')->whereIn('category_cid',array(9,24,16,19))->limit(6)->get();
        View::with('shang',$shang);
 	
        // 下装
        $xia=Db::table('goods')->whereIn('category_cid',array(28,21,22,30))->limit(6)->get();
        View::with('xia',$xia);

        // 更多精品
        $geng=Db::table('goods')->whereIn('category_cid',array(13,15,27))->limit(5)->get();
        View::with('geng',$geng);

       	View::make();
    }

    
    // 购物车异步  显示
    public function ajaxCart(){
        if(!IS_AJAX) return FALSE;
        // 转换session中的goods数组
        $data=array();
        if(isset($_SESSION['cart'])){
            $data=$_SESSION['cart'];
        }
        View::ajax($data);
    }

    // 购物车异步 删除
    public function ajaxDel(){
        $data=array();
        if(IS_AJAX){
            $temp=array();
            // 得到商品序号
            $temp['sid']=Q('post.gid');
            $temp['num']=0;
            // p($gid);
            // 删除session中对应的商品
           Cart::update($temp);
           $data['total_rows']=$_SESSION['cart']['total_rows'];
           $data['total']=$_SESSION['cart']['total'];
        }
        // 分配回去
        View::ajax($data);
    }


}
